package com.fangcun.controller.admin.old;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fangcun.bean.sys.PageBean;
import com.fangcun.bean.sys.ResponseMsg;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.util.PublicMethod;
import com.fangcun.common.util.apiversion.ApiVersion;
import com.fangcun.entity.Advertising;
import com.fangcun.service.admin.SysAdvertisingService;

@RestController
@RequestMapping("/admin/{version}/auth/advertising")
public class AdvertisingController {
	private static Logger LOG = LoggerFactory.getLogger(AdvertisingController.class);

	@Autowired
	private SysAdvertisingService sysAdvertisingService;

	/**列表**/
	@ApiVersion(1.0)
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	private ResponseMsg list(@RequestBody PageBean<Advertising> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(sysAdvertisingService.list(request));
		} catch (ProException e) {
			LOG.error("-后台广告列表->{},{}", e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台广告列表->{}", PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 详情 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	private ResponseMsg details(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(sysAdvertisingService.details(id));
		} catch (ProException e) {
			LOG.error("-广告详情->{},{}", e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-广告详情->{}", PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 修改 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	private ResponseMsg update(@RequestBody Advertising request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			sysAdvertisingService.update(request);
		} catch (ProException e) {
			LOG.error("-修改广告->{},{}", e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-修改广告->{}", PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 增加 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	private ResponseMsg add(@RequestBody Advertising request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			sysAdvertisingService.add(request);
		} catch (ProException e) {
			LOG.error("-发布广告->{},{}", e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-发布广告->{}", PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	private ResponseMsg delete(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			sysAdvertisingService.delete(id);
		} catch (ProException e) {
			LOG.error("-删除广告->{},{}", e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-删除广告->{}", PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
